package com.openmanus.agent.core;

import org.springframework.context.ApplicationEvent;

import java.time.LocalDateTime;

/**
 * 智能体状态变更事件
 * 当智能体状态发生变化时发布此事件
 * 
 * @author OpenManus Team
 * @version 1.0.0
 */
public class AgentStateChangeEvent extends ApplicationEvent {

    private final BaseAgent agent;
    private final AgentState fromState;
    private final AgentState toState;
    private final LocalDateTime timestamp;

    public AgentStateChangeEvent(BaseAgent source, AgentState fromState, AgentState toState) {
        super(source);
        this.agent = source;
        this.fromState = fromState;
        this.toState = toState;
        this.timestamp = LocalDateTime.now();
    }

    /**
     * 获取智能体实例
     * 
     * @return 智能体实例
     */
    public BaseAgent getAgent() {
        return agent;
    }

    /**
     * 获取变更前状态
     * 
     * @return 变更前状态
     */
    public AgentState getFromState() {
        return fromState;
    }

    /**
     * 获取变更后状态
     * 
     * @return 变更后状态
     */
    public AgentState getToState() {
        return toState;
    }

    /**
     * 获取事件时间戳
     * 
     * @return 事件时间戳
     */
//    public LocalDateTime getTimestamp() {
//        return timestamp;
//    }

    /**
     * 获取智能体名称
     * 
     * @return 智能体名称
     */
    public String getAgentName() {
        return agent.getName();
    }

    /**
     * 检查是否为指定的状态转换
     * 
     * @param from 源状态
     * @param to 目标状态
     * @return 是否匹配
     */
    public boolean isTransition(AgentState from, AgentState to) {
        return this.fromState == from && this.toState == to;
    }

    /**
     * 检查是否转换到指定状态
     * 
     * @param state 目标状态
     * @return 是否转换到指定状态
     */
    public boolean isTransitionTo(AgentState state) {
        return this.toState == state;
    }

    /**
     * 检查是否从指定状态转换
     * 
     * @param state 源状态
     * @return 是否从指定状态转换
     */
    public boolean isTransitionFrom(AgentState state) {
        return this.fromState == state;
    }

    @Override
    public String toString() {
        return String.format("AgentStateChangeEvent{agent='%s', from=%s, to=%s, timestamp=%s}",
            agent.getName(), fromState, toState, timestamp);
    }
}
